App({
  globalData: {
    userInfo: null,
    userType: null // 'customer', 'dealer', 'admin', 'sales'
  },
  onLaunch() {
    // 确保globalData对象存在
    if (!this.globalData) {
      this.globalData = {
        userInfo: { nickName: '用户' },
        userType: 'customer'
      }
    }
    
    // 获取用户信息
    try {
      wx.getSetting({
        success: res => {
          if (res && res.authSetting && res.authSetting['scope.userInfo']) {
            wx.getUserInfo({
              success: res => {
                if (res && res.userInfo) {
                  this.globalData.userInfo = res.userInfo
                }
                this.checkUserType()
              },
              fail: () => {
                this.globalData.userInfo = { nickName: '用户' }
                this.checkUserType()
              }
            })
          } else {
            this.globalData.userInfo = { nickName: '用户' }
            this.checkUserType()
          }
        },
        fail: () => {
          this.globalData.userInfo = { nickName: '用户' }
          this.checkUserType()
        }
      })
    } catch (error) {
      console.error('应用启动失败:', error)
      this.globalData.userInfo = { nickName: '用户' }
      this.globalData.userType = 'customer'
    }
  },
  checkUserType() {
    // 根据接口判断用户类型，使用模拟数据避免请求失败
    // 实际项目中应该替换为真实的API地址
    try {
      // 模拟请求，默认设置为普通用户
      setTimeout(() => {
        this.globalData.userType = 'customer'
      }, 100)
    } catch (error) {
      console.error('用户类型检查失败:', error)
      this.globalData.userType = 'customer' // 默认设置为普通用户
    }
  }
})